﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ValidationHelper.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the ValidationHelper type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Utils
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;

    /// <summary>
    /// </summary>
    public static class ValidationHelper
    {
        /// <summary>
        /// Verifies the object argument.
        /// </summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="argumentName">Name of the argument.</param>        
        public static void VerifyObjectArgument(object objectToValidate, string argumentName)
        {   
            if (objectToValidate == null)
            {
                throw new ArgumentNullException("'" + argumentName + "'" + Messages.IsNullOrEmpty);
            }

            // HE: We cannot do this because not all objects can be converted into a string
            //TraceValidation(objectToValidate.ToString(), argumentName);
        }

        /// <summary>
        /// Verifies that the argument is serializable.
        /// </summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="argumentName">Name of the argument.</param>
        public static void VerifySerializableArgument(object objectToValidate, string argumentName)
        {
            if (objectToValidate.GetType().IsSerializable == false)
            {
                throw new ArgumentNullException("'" + argumentName + "' cannot be serialized.  " +
                                    "Consider adding the 'Serializable' attribute to your class.");
            }

            // HE: We cannot do this because not all objects can be converted into a string
            //TraceValidation(objectToValidate.ToString(), argumentName);

        }

        /// <summary>
        /// Verifies the string argument.
        /// </summary>
        /// <param name="stringToValidate">The string to validate.</param>
        /// <param name="argumentName">Name of the argument.</param>        
        public static void VerifyStringArgument(string stringToValidate, string argumentName)
        {
            if (string.IsNullOrEmpty(stringToValidate))
            {
                throw new ArgumentException("'" + argumentName + "'" + Messages.IsNullOrEmpty);
            }

            TraceValidation(stringToValidate, argumentName);
        }

        private static void TraceValidation(string stringToValidate, string argumentName)
        {
            string msg = String.Format("Argument '{0}' set to this value '{1}",
                            argumentName,
                            StringHelper.EscapeForFormat(stringToValidate));

            TraceSwitch traceSwitch = new TraceSwitch("LibraryTrace", "Trace switch for the library");

            TraceHelper.TraceVerbose(traceSwitch, "ValidationHelper.VerifyStringArgument", msg);
        }

        /// <summary>
        /// Verifies that the Guid is not Empty.
        /// </summary>
        /// <param name="stringToValidate">The string to validate.</param>
        /// <param name="argumentName">Name of the argument.</param>        
        public static void VerifyGuidArgument(Guid guidToValidate, string argumentName)
        {
            if (guidToValidate == Guid.Empty)
            {
                throw new ArgumentException("'" + argumentName + "'" + Messages.IsNullOrEmpty);
            }

            TraceValidation(guidToValidate.ToString(), argumentName);
        }

        /// <summary>
        /// Verifies that the int argument is not zero
        /// </summary>
        /// <param name="argumentValue">The integer to validate</param>
        /// <param name="argumentName">Name of the argument.</param>        
        public static void VerifyIntArgument(int argumentValue, string argumentName)
        {
            if (argumentValue == 0)
            {
                throw new ArgumentException("'" + argumentName + "'" + Messages.IsNullOrEmpty);
            }

            TraceValidation(argumentValue.ToString(), argumentName);
        }
    }
}
